<?php

class cntrlbook implements viewController {
	private $area;
	private $side;
	private $dependencies = array("JavaScript" => array("facebox.js", "book.js"), "CSS" => array("facebox.css") );
	private $pageName = "PicBoard Book Viewer";
	
	private $userCharts;
	private $userComittedCharts = array();
	private $bookCharts;
	private $groupCharts;
	private $bookChartsIDs = array();
	private $bookInfo;

	public function __construct(user &$userObj){

		if( $userObj->getAccessLevel() == -1 ){
			// User is not logged in
			header("Location: home");
			exit;
		}
		
		parse_str( $_GET['act'] );
		
		// Main area
		$this->area = new template("area.bookGrid");
		
		// Side area.
		$this->side = new template("control.bookOptions");	
		
		$this->userCharts = chart::getUserSharableCharts( $userObj->getuID() );
		
		// Are we loading a book?
		if(isset($id)){
			
			$this->bookCharts = chart::getBookCharts($id);
			
			foreach($this->bookCharts as $chart){
				$this->bookChartsIDs[] = $chart['chID'];
			}
			
			$this->bookInfo = book::getBook($userObj, $id);
			
			if( count($this->bookInfo) > 0){
				
				// Does the requested ID exist?
							
				// Check permissions
				if($this->bookInfo['user_uID'] != $userObj->getuID() && $this->bookInfo['private'] == 1) header("Location: error?e=166");
				if($this->bookInfo['private'] == 1 && $this->bookInfo['group_gID'] != "" && $this->bookInfo['active'] == 0) header("Location: error?e=166");
				
				// Is this a group? If so, add the group's charts to the list as well as just the user
				if($this->bookInfo['group_gID'] != ""){
					$this->groupCharts = chart::getGroupCharts( $this->bookInfo['group_gID'] );
					$this->buildPage(true);
				} else {
					$this->buildPage();
				}
			
			} else {
				// ID doesnt exist. Send the user to the build new book page.
				header("Location: book");	
			}
			
		} else {
			$this->bookInfo['name'] = "<input id='book_namebox' />";
			$this->buildPage();
		}	
		
	}
	
	public function buildPage($groupCharts = false){
		// Get the name for the book and write it to the page 
		$this->area->replace("bookName", $this->bookInfo['name']);
		
		// Write out the used charts
		$this->area->replace("charts", $this->generateContentArea() );
		
		if($groupCharts == true){
			$output = $this->generateGroupChartThumbs();
			//$output .= $this->generateUserChartThumbs(true);
			
			$this->side->replace("charts", $output );
		} else {
			$this->side->replace("charts", $this->generateUserChartThumbs() );
		}
		
	}
	
	private function generateUserChartThumbs($header = false){
	
		$outputFormat = "<div id=\"chartItemContainer\">%s</div>";
		$itemFormat = "<div class=\"chartResult clearer ui-corner-all userChart\" id=\"ch%s\"><h3>%s</h3><p>%s</p><p><a href='chart?id=%d'>View Chart</a></p></div>\n";
		$output = "";
		
		foreach($this->userCharts as $chart){
			if(in_array($chart['chID'], $this->bookChartsIDs) == false) $output .= sprintf($itemFormat, $chart['chID'], $chart['name'], $chart['description'], $chart['chID']);
		}
	
		$this->dependencies["InlineJavaScript"][] = sprintf("var comittedCharts = %s;", json_encode($this->userComittedCharts));
	
		if($header) $outputFormat = "<h3>Your Charts</h3>" . $outputFormat;
		
		return sprintf($outputFormat, $output);
		
	}
	
	private function generateGroupChartThumbs(){
		$itemFormat = "<div class=\"chartResult clearer ui-corner-all groupChart\" id=\"ch%s\"><h3>%s</h3><p>%s</p><p><a href='chart?id=%d'>View Chart</a></p></div>\n";
		$output = "";
		
		foreach($this->groupCharts as $chart){
			if(in_array($chart['chID'], $this->bookChartsIDs) == false) $output .= sprintf($itemFormat, $chart['chID'], $chart['name'], $chart['description'], $chart['chID']);
		}
		
		return sprintf("<h3>Group Charts</h3><div id=\"chartItemContainer\">%s</div>", $output);	
	}
	
	private function generateContentArea(){
	
		$itemFormat = "<div class=\"chartActiveResult clearer ui-corner-all\" id=\"ach%s\"><a class='chartRemove' href='#'><img src=\"../images/close.png\"></a><h3>%s</h3><p>%s</p><p><a href='chart?id=%d'>View Chart</a></p></div>\n";
		$output = "";
		
		if(count($this->bookCharts) > 0){
			
			foreach($this->bookCharts as $chart){
				$output .= sprintf($itemFormat, $chart['chID'], $chart['name'], $chart['description'], $chart['chID']);
			}
			
			return sprintf("<div id=\"activeChartItemContainer\">%s</div>", $output);
		} else {
			return "<div id=\"activeChartItemContainer\"></div><div id=\"infoPane\" style=\"display:table; height:100%; width:100%\"><div id=\"PBAreaExplain_text\"><var id=\"lang_76_bookInstructions\"></var></div></div>";
		}
	}
	
	public function getSide(){
		return $this->side;
	}
	
	public function getArea(){
		return $this->area;
	}
	
	public function getDependencies(){
		return $this->dependencies;
	}
	
	public function getPageName(){
		return $this->pageName;
	}

	public function getTools(){
		return new template("toolset.book");
	}

}

?>